package com.example.helloword;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionManager {
    private String[] permissions;

    public PermissionManager(String[] permissions) {
        this.permissions = permissions;
    }
    /**
     * 申请权限
     */
    public void checkPermissions(Activity activity,int requestCode){
        //如果系统大于android6.0，进行动态权限申请
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            for (int i=0;i<permissions.length;i++){
                if (ContextCompat.checkSelfPermission(activity, permissions[i])!= PackageManager.PERMISSION_GRANTED){
                    startRequestPermission(activity,requestCode);
                    break;
                }
            }
        }
    }

    /**
     * 通过权限列表，提示用户赋予或禁止当前还未拥有的权限
     */
    private void startRequestPermission(Activity activity,int requestCode) {
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }
}
